package hotel.meituan.oversea.biz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.vo.meituan.request.order.MtHotelOrderCheckParamVo;
import hotel.meituan.oversea.api.dto.MtHotelOrderBookingDealingDto;
import hotel.meituan.oversea.api.entity.MtHotelOrderBooking;
import hotel.meituan.oversea.api.entity.MtHotelOrderStateHistory;
import hotel.meituan.oversea.api.vo.down.request.order.*;
import hotel.meituan.oversea.biz.service.MtHotelOrderDealService;
import hotel.meituan.oversea.biz.service.MtHotelOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 美团酒店信息Controller
 * @author qiuyy
 * @date 2020-05-15
 */
@Slf4j
@RestController
@RequestMapping("/mtHotelOrder")
@Api(value = "MtHotelOrderController", tags = {"美团酒店订单接口"})
public class MtHotelOrderController {
    @Autowired
    private MtHotelOrderService mtHotelOrderService;
    @Autowired
    private MtHotelOrderDealService mtHotelOrderDealService;

    @PostMapping("/getPageHotelOrderDealing")
    @ApiOperation(value = "获取处理中订单(确认中,取消中)",notes="必须经过auth授权")
    public Result<Object> getHotelOrderDealing(@RequestBody JSONObject jsonObject){
        //参数校验
        String gtTime = (String)jsonObject.get("gtTime");
        if(null == gtTime){
            return new Result<>("gtTime",CommonConstants.REQUEST_ERROR);
        }
        return mtHotelOrderService.getHotelOrderDealing(gtTime);
    }

    @PostMapping("/getPageHotelOrderDealingForReminder")
    @ApiOperation(value = "获取处理中订单(确认中,取消中)为催单",notes="必须经过auth授权")
    public Result<Object> getPageHotelOrderDealingForRemind(){
        return mtHotelOrderService.getPageHotelOrderDealingForReminder();
    }

    @PostMapping("/updateReminderCount")
    @ApiOperation(value = "更新催单次数",notes="必须经过auth授权")
   public Result<Object> updateReminderCount(@RequestBody Map map){
        log.info("更新催单次数 updateReminderCount={}", JSON.toJSONString(map));
        String mtOrderId = (String)map.get("mtOrderId");
        String isReminderRemark = (String)map.get("isReminderRemark");
        return mtHotelOrderService.updateReminderCount(mtOrderId,isReminderRemark);
    }


    /**
     *  [预约下单接口]要上送自定义distributorOrderId
     *  下单后上游会返回distributorOrderid和上游平台订单ID号mtOrderId
     *  [查询订单接口]根据  idstrbutorOrderId和mtOrderId
     * @param mtHotelOrderParamVo
     * @return
     */
    @PostMapping("/orderCheck")
    @ApiOperation(value = "下单前校验",notes="必须经过auth授权")
    public Result<Object> aspcheckOrder(@RequestBody MtHotelOrderCheckParamVo mtHotelOrderParamVo){
        //下单前校验接口，地址：https://developer-distribution.meituan.com/distribution-open-platform/#/apiDocs/1/25/26
        return mtHotelOrderService.orderCheckApi(mtHotelOrderParamVo);
    }

    /**
     * 预约订单，当前只是提交订单数据，上游返回是否创建订单成功，
     * 订单确认（酒店确认）需等待异步通知或者查询订单接口主动查询
     *
     * @param mtHotelOrderBookingParamVo
     * @return
     */
    @PostMapping("/orderBooking")
    @ApiOperation(value = "预约下单",notes="必须经过auth授权")
    public Result<Object> orderBooking(@RequestBody MtHotelOrderBookingParamVo mtHotelOrderBookingParamVo){
        //校验参数
        //先保存预约下单信息
        //MtHotelOrderBooking mtHotelOrderBooking = mtHotelOrderService.saveRequsetOrderBooking(mtHotelOrderBookingParamVo);
       /* if(null != mtHotelOrderBooking){

        }else{
            return new Result<>("保存预约下单数据异常", CommonConstants.REQUEST_ERROR);
        }*/
        //提交订单

        /*if(null == mtHotelOrderBookingParamVo.getMtHotelOrderCheckVo()){
            return new Result<>("mtHotelOrderCheckVo 不可为空", CommonConstants.REQUEST_ERROR);
        }*/
        return mtHotelOrderService.orderBookingApi(mtHotelOrderBookingParamVo,null);
    }

    @PostMapping("/orderCancel")
    @ApiOperation(value = "下单取消",notes="必须经过auth授权")
    public Result<Object> orderCancel(@RequestBody MtHotelOrderCancelParamVo mtHotelOrderCancelParamVo){
        //取消订单,接口:https://developer-distribution.meituan.com/distribution-open-platform/#/apiDocs/1/25/28
        //调用上游取消接口前，判断订单状态，接收订单状态为 未确认和已确认
        //已确认的订单，需人工处理，不调用上游接口，把订单保存到 人工处理取消订单表

        if(null == mtHotelOrderCancelParamVo.getDistributorOrderId()){
            return new Result<>("distributorOrderId 不可为空",CommonConstants.REQUEST_ERROR);
        }
        if(null == mtHotelOrderCancelParamVo.getMtOrderId()){
            return new Result<>("mtOrderId 不可为空",CommonConstants.REQUEST_ERROR);
        }
        Result<Object> reApi =  mtHotelOrderService.orderCancelApi(mtHotelOrderCancelParamVo);
        return reApi;
    }


    /**
     * <H1>取消美团</H1>
     *
     * @param orderId 实体对象
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @Date 2020/9/7 15:00
     */
    @GetMapping("/hhbCancelMt")
    public Result<Object> cancelMt(String orderId) {
        if(ObjectUtils.isEmpty(orderId)){//携程订单id 为美团订单表中swOrderId
            return  new Result<>("orderId 不可为空", CommonConstants.REQUEST_ERROR);
        }
        return mtHotelOrderService.hhbCancelMt(orderId);
    }

    @PostMapping("/orderQuery")
    @ApiOperation(value = "单个订单查询",notes="必须经过auth授权")
    public Result<Object> orderQuery(@RequestBody MtHotelOrderQueryParamVo mtHotelOrderQueryParamVo){
        //校验参数
        return mtHotelOrderService.orderQuery(mtHotelOrderQueryParamVo);
    }
    @PostMapping("/orderQueryBatch")
    @ApiOperation(value = "批量订单查询",notes="必须经过auth授权")
    public Result<Object> orderQueryBatch(@RequestBody JSONObject jsonObject){
        List<MtHotelOrderQueryParamVo> mtHotelOrderQueryParamVoList = JSON.parseArray(jsonObject.get("mtHotelOrderQueryParamVoList").toString(), MtHotelOrderQueryParamVo.class);
        //校验参数
        return mtHotelOrderService.orderQueryBatch(mtHotelOrderQueryParamVoList);
    }

    @PostMapping("/queryDealingOrderAndUpdateOrder")
    @ApiOperation(value = "查询处理中订单并更新本地订单状态",notes="必须经过auth授权")
    public Result<Object> quartzQueryDealingOrderAndUpdateOrder(@RequestBody JSONObject jsonObject){
        //内部外放接口：订单查询，不限制订单上送数量
        //接口内部做判断，由于上游接口有一分钟次数限制，
        //所以该接口做判断次数然后sleep

        List<MtHotelOrderBookingDealingDto> dtoIds = JSON.parseArray(JSON.toJSONString(jsonObject.get("dtoIds")), MtHotelOrderBookingDealingDto.class);
        return mtHotelOrderService.quartzQueryDealingOrderAndUpdateOrder(dtoIds);
    }

    @PostMapping("/orderHotelReminder")
    @ApiOperation(value = "酒店催单",notes="必须经过auth授权")
    public Result<Object> orderHotelReminder(@RequestBody MtHotelOrderReminderParamVo MtHotelOrderReminderParamVo){
        //校验参数
        return mtHotelOrderService.hotelReminder(MtHotelOrderReminderParamVo);
    }

    @PostMapping("/jobQueryOrderCheckSave")
    @ApiOperation(value = "定时器主动查询订单符合条件订单进行更新或美团异步通知对该订单进行状态等更新",notes="必须经过auth授权")
    public Result<Object> jobQueryOrderCheckSave(@RequestBody Map map){
        String source = map.get("source").toString();
        MtHotelOrderJobQueryOrderVo mtHotelOrderJobQueryOrderVo = JSON.parseObject((String)map.get("mtHotelOrderJobQueryOrderVo"), MtHotelOrderJobQueryOrderVo.class);
        MtHotelOrderStateHistory mtHotelOrderStateHistory = JSON.parseObject((String)map.get("stateHistory"), MtHotelOrderStateHistory.class);
        log.info("jobQueryOrderCheckSave map="+ JSON.toJSONString(map));
        return mtHotelOrderService.jobQueryOrderCheckSave(mtHotelOrderJobQueryOrderVo,mtHotelOrderStateHistory,source);
    }

    @GetMapping("/selectOneBySwOrderIsVail")
    @ApiOperation(value = "查询很好办美团订单根据携程id",notes="必须经过auth授权")
    public Result<Object> selectOneBySwOrderIsVail(String swOrderId){
        List<MtHotelOrderBooking> list =  mtHotelOrderService.selectOneBySwOrderIsVail(swOrderId);
        if(null != list && list.size()>0){
            return new Result<>(list.get(0));
        }else{
            return new Result<>("未找到美团订单",CommonConstants.REQUEST_ERROR);
        }
    }
    /***
     * @Author: lmf
     * @Date: 2021/5/6
     * 获取延迟单列表
     */
    @PostMapping("/getDealOrderList")
    @ApiOperation(value = "获取延迟单列表",notes="必须经过auth授权")
    public Result<Object> getDealOrderList(@RequestBody Map map){
        int pageCode=1;
        if(map.containsKey("pageCode")){
            pageCode= (int) map.get("pageCode");
        }

        int pageSize= 15;
        if(map.containsKey("pageSize")){
            pageSize= (int) map.get("pageSize");
        }
        return mtHotelOrderDealService.getDealOrderList(pageCode,pageSize,map);
    }
    /***
     * @Author: lmf
     * @Date: 2021/5/8
     * 获取延时单数量
     */
    @PostMapping("/getDealOrderNum")
    @ApiOperation(value = "获取延时单数量",notes="必须经过auth授权")
    public Result<Object> getDealOrderNum(@RequestBody Map map){
        log.info("====================请求的数据为{}=======", JSON.toJSONString(map));
        return mtHotelOrderDealService.getDealOrderNum(map);
    }
    /**
     * 获取订单是否为拆单类型
     */
    @PostMapping("/getOrderType")
    @ApiOperation(value = "获取延时单数量",notes="必须经过auth授权")
    public Result<Object> getOrderType(@RequestBody Map map){
        log.info("====================请求的获取订单是否为拆单类型数据为{}=======", JSON.toJSONString(map));
        return mtHotelOrderService.getOrderType(map);
    }
}
